home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / MacOS / GraphicalTool / MacZStringFileSpec.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  2.0 KB  |  113 lines

  1. /*==================================================================
  2.     File:        MacZStringFileSpec.h
  3.  
  4.     Contains:    Helper class for storing and managing a file
  5.                 spec within the ZString tool.
  6.  
  7.     Written by:    Eric Traut
  8.  
  9.     Copyright:    2000-2001 Connectix Corporation
  10.     
  11.     This source has been placed into the public domain by
  12.     Connectix Corporation. You have the right to modify, 
  13.     distribute or use this code without any legal limitations
  14.     or finanicial/licensing requirements. Connectix is not 
  15.     liable for any problems that result from the use of this 
  16.     code.
  17.     
  18.     If you have comments, feedback, questions, or would like
  19.     to submit bug fixes or updates to this code, please email
  20.     opensource@connectix.com.
  21. ==================================================================*/
  22.  
  23. #pragma once
  24.  
  25. #include <Icons.h>
  26. #include <Files.h>
  27.  
  28.  
  29. class MacZStringFileSpec
  30. {
  31.     public:
  32.         MacZStringFileSpec()
  33.         {
  34.             mIconRef = NULL;
  35.             mSpecIsValid = false;
  36.         }
  37.         
  38.         ~MacZStringFileSpec()
  39.         {
  40.             ReleaseIconRef();
  41.         }
  42.         
  43.         void
  44.         ReleaseIconRef()
  45.         {
  46.             if (::IsValidIconRef(mIconRef))
  47.                 ::ReleaseIconRef(mIconRef);
  48.             mIconRef = NULL;
  49.         }
  50.  
  51.         void
  52.         SetFileSpec(
  53.             const FSSpec &        inFileSpec)
  54.         {
  55.             mFileSpec = inFileSpec;
  56.             mSpecIsValid = true;
  57.             ReleaseIconRef();
  58.         }
  59.         
  60.         void
  61.         ClearFileSpec()
  62.         {
  63.             mSpecIsValid = false;
  64.             ReleaseIconRef();
  65.         }
  66.         
  67.         Boolean
  68.         IsValid() const
  69.         {
  70.             return mSpecIsValid;
  71.         }
  72.         
  73.         const FSSpec & 
  74.         GetFSSpec()
  75.         {
  76.             return mFileSpec;
  77.         }
  78.         
  79.         operator const FSSpec & ()
  80.         {
  81.             return mFileSpec;
  82.         }
  83.         
  84.         IconRef
  85.         GetIconRef()
  86.         {
  87.             if (mIconRef == NULL)
  88.             {
  89.                 SInt16 label;
  90.                 
  91.                 // Make sure the file exists first.
  92.                 FSSpec dummySpec;
  93.                 OSErr err = ::FSMakeFSSpec(mFileSpec.vRefNum, mFileSpec.parID, mFileSpec.name, &dummySpec);
  94.                 
  95.                 if (err == noErr)
  96.                 {
  97.                     err = ::GetIconRefFromFile(&mFileSpec, &mIconRef, &label);
  98.                     SignalIf_(err != noErr);
  99.                 }
  100.             }
  101.  
  102.             return mIconRef;
  103.         }
  104.         
  105.     private:
  106.         FSSpec        mFileSpec;
  107.         Boolean        mSpecIsValid;
  108.         IconRef        mIconRef;
  109. };
  110.  
  111.  
  112.  
  113.